package com.myh.testlogin.config;

import com.myh.testlogin.config.interceptor.LoginInterceptor;
import com.myh.testlogin.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 莫耀华
 * @date 2020-09-19 9:28
 */
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;


    @Override
    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                  //允许跨越发送cookie
//                .allowedOrigins("*")
//                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
//                //放行全部原始头信息
//                .allowCredentials(true)
//                .maxAge(3600)
//                //允许所有请求方法跨域调用
//                .allowedHeaders("*");
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                //拦截
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/login");
//                .excludePathPatterns("/user/updatePwdById/**");
    }
}
